﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcApplication1.Models;
using System.Diagnostics;

namespace MvcApplication1.Controllers
{
    public class AdminSessionCheckAttribute : ActionFilterAttribute
    {
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            
            if (filterContext.HttpContext.Session.IsNewSession && filterContext.HttpContext.Request.IsAuthenticated && filterContext.ActionDescriptor.ActionName != "Index")
            {

                (filterContext.Controller as Controller).Session["Admin"] = new Admin();
            }
            base.OnActionExecuting(filterContext);
        }

    }

    [Authorize(Roles="admin")]
    [AdminSessionCheck]
    public class AdminController : Controller
    {
        private Admin admin;
        //
        // GET: /Admin/

        public ActionResult Index(string sortorder, int? page, string brandname)
        {
            if (Session["Admin"] == null)
            {
                admin = new Admin();
                Session["Admin"] = admin;
            }
            else
                admin = Session["Admin"] as Admin;
            IQueryableBrandsManagerSearch ms = new IQueryableBrandsManagerSearch(admin.Brands, brandname);
            IQueryable<Brand> brandview = ms.SearchView;
            if (string.IsNullOrEmpty(sortorder) == false)
            {
                IQueryableBrandsSorter sorter = new IQueryableBrandsSorter();
                sorter.Sort(ref brandview, sortorder);
                ViewBag.sortorder = sortorder;
            }
            else
            {
                ViewBag.sortorder = "";
            }
            int pagenumber = page ?? 1;
            IQueryableBrandsPager pager = new IQueryableBrandsPager(brandview, pagenumber, 5);
            ViewBag.pagecount = pager.PageCount;
            ViewBag.pagenumber = pagenumber;
            ViewBag.brandlist = pager.CurrentPage;
            ViewBag.brandname_search = brandname;

            return View();
        }

        public ActionResult AdminEditBrand(string brandid)
        {
            Int64 brandID = Convert.ToInt64(brandid);
            AccountMembershipService ams = new AccountMembershipService();
            MembershipBrand brand = ams.GetBrandFromID(brandID);
            Manage ma = new Manage(brand);
            Session["manage"] = ma;
            return RedirectToAction("UserBrand", "Manage");
        }

        public ActionResult AdminEditItem(string brandid)
        {
            Int64 brandID = Convert.ToInt64(brandid);
            AccountMembershipService ams = new AccountMembershipService();
            MembershipBrand brand = ams.GetBrandFromID(brandID);
            Manage ma = new Manage(brand);
            Session["manage"] = ma;
            return RedirectToAction("UserManage", "Manage");
        }

        public ActionResult UserAccountManage(int? page,string username)
        {
            UserAccount ua = new UserAccount();
            IQueryableUsersSearch us = new IQueryableUsersSearch(ua.Users, username);
            IQueryable<User> userview = us.SearchView;
            int pagenumber = page ?? 1;
            IQueryableUserPager pager = new IQueryableUserPager(userview, pagenumber, 5);
            ViewBag.pagecount = pager.PageCount;
            ViewBag.pagenumber = pagenumber;
            ViewBag.userlist = pager.CurrentPage;
            ViewBag.username_search = username;
            return View();
        }

        public ActionResult AdminEditUser(string username)
        {
            UserAccount ua = new UserAccount(username);
            MvcApplication1.Models.User currentuser = null;
            foreach (MvcApplication1.Models.User user in ua.Users)  //ua include only one user
                currentuser = user;
            return View(currentuser);
        }

        [HttpPost]
        public ActionResult AdminEditUser(string username, bool isapproved, string role)
        {
            UserAccount ua = new UserAccount(username);
            MvcApplication1.Models.User currentuser = null;
            foreach (MvcApplication1.Models.User user in ua.Users)  //ua include only one user
                currentuser = user;
            currentuser.isapproved = isapproved;
            currentuser.role = role;
            ua.SaveChange();
            if(isapproved==true)
                ((DataToSearch)HttpContext.Application["datatosearch"]).AddBrand(username);
            else
                ((DataToSearch)HttpContext.Application["datatosearch"]).DelBrand(username);
            ViewBag.changesuccess = true;
            return View(currentuser);
        }

    }
}
